package aize.corp.freeconso;

import java.util.LinkedList;

import model.Setting;
import database.DBAdapter;
import aize.corp.freeconso.R;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
 
public class WidgetConfigure extends Activity {
 
    private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    private DBAdapter database = new DBAdapter(this);
	private LinkedList<Setting> settings = new LinkedList<Setting>();
	
    /** Called when the activity is created */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widget_config);
        setResult(RESULT_CANCELED);
        
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }
 
        // If they gave us an intent without the widget id, just bail.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            Log.e("Napply", "Configuration Activity: no appwidget id provided");
            finish();
        }
        
    	database.open();
    	if(database.countSetting() == 0) {
    		Toast.makeText(getApplicationContext(), "Merci de configurer vos identifiants !", Toast.LENGTH_SHORT).show();
    		database.close();
    		finish();  
    	}
    	else {
    		settings = database.getSettings();
    		database.close();
    		TextView texteView = (TextView) findViewById(R.id.textViewTitle);
            Typeface font = Typeface.createFromAsset(getAssets(), "ariblk.ttf");
            texteView.setTypeface(font); 
    		
            UpdateDisplay();
    	}
    }
 
    private void UpdateDisplay() {
    	Spinner spin = (Spinner) findViewById(R.id.spinner);
    	String items[] = new String[settings.size()];
    	for(int i=0;i<settings.size();i++) {
    		items[i] = settings.get(i).getName();
    	}
    	final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adapter);
		
		Spinner spinTime = (Spinner) findViewById(R.id.spinnerTime);
    	String[] timePeriod = getResources().getStringArray(R.array.widget_timeperiod);
    	final ArrayAdapter<String> adapterTime = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, timePeriod);
    	adapterTime.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spinTime.setAdapter(adapterTime);
    	
    	Button button = (Button) findViewById(R.id.buttonSave);
    	button.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				configureWidget(getApplicationContext());
				Intent resultValue = new Intent();
		        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
		        setResult(RESULT_OK, resultValue);
		        finish(); 
			}});
	}

    public void configureWidget(Context context) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        
        Spinner spin = (Spinner) findViewById(R.id.spinner);
        Spinner spinTime = (Spinner) findViewById(R.id.spinnerTime);
        
        Widget.updateAppWidget(context, appWidgetManager, mAppWidgetId, spinTime.getSelectedItemPosition(), spin.getSelectedItemPosition());
    }
}